package com.mxpio.erp.equipment.controller;


import com.mxpio.erp.common.equipment.entity.RepairPickupNoticeLine;
import com.mxpio.erp.common.equipment.service.RepairPickupNoticeLineService;
import com.mxpio.erp.common.jpa.model.NoticeLineKey;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "RepairPickupNoticeLineController", description = "设备维修领料通知单行接口")
@RestController
@RequestMapping("/erp/equipment/spotrepair/pickupnotice/line/")
public class RepairPickupNoticeLineController {

    @Autowired
    private RepairPickupNoticeLineService service;

    @GetMapping("list")
    @Operation(summary = "维修领料通知单行列表", description = "获取维修领料通知单行列表（无分页）", method = "GET")
    public Result<List<RepairPickupNoticeLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<RepairPickupNoticeLine> noticeLines = service.list(RepairPickupNoticeLine.class, criteria);
        return Result.OK(noticeLines);
    }

    @GetMapping("list/{noticeNo}")
    @Operation(summary = "根据noticeNo获取维修领料通知单行列表", description = "根据noticeNo获取获取维修领料通知单行列表（无分页）", method = "GET")
    public Result<List<RepairPickupNoticeLine>> listByBizNo(@Parameter(description="通知单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) throws UnsupportedEncodingException {
        Criteria c = Criteria.create().addCriterion("noticeNo", Operator.EQ, noticeNo);
        List<RepairPickupNoticeLine> noticeLines = service.list(RepairPickupNoticeLine.class, c);
        return Result.OK(noticeLines);
    }

    @GetMapping("page")
    @Operation(summary = "维修领料通知单行列表", description = "获取维修领料通知单行列表（分页）", method = "GET")
    public Result<Page<RepairPickupNoticeLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<RepairPickupNoticeLine> noticeLines = service.listPage(RepairPickupNoticeLine.class, page, criteria);
        return Result.OK(noticeLines);
    }

    @GetMapping("list/{noticeNo}/{lineNo}")
    @Operation(summary = "根据ID获取维修领料通知单行", description = "根据ID获取维修领料通知单行", method = "GET")
    public Result<RepairPickupNoticeLine> getById(@Parameter(description="通知单号") @PathVariable(name = "noticeNo", required = true) String noticeNo,@Parameter(description="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        NoticeLineKey lineKey = new NoticeLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setNoticeNo(noticeNo);
        RepairPickupNoticeLine repairReturnBomNoticeLine = service.getById(RepairPickupNoticeLine.class, lineKey);
        return Result.OK(repairReturnBomNoticeLine);
    }
}
